package cn.xiuxius.embedded.command;

import cn.xiuxius.embedded.command.build.BuildCommand;
import cn.xiuxius.embedded.command.clean.CleanCommand;
import cn.xiuxius.embedded.command.converter.ExistingDirectoryConverter;
import cn.xiuxius.embedded.command.flash.FlashCommand;
import cn.xiuxius.embedded.command.preprocess.ProcessCommand;
import cn.xiuxius.embedded.command.run.RunCommand;
import cn.xiuxius.embedded.config.EasyYamlConfig;
import cn.xiuxius.embedded.keil.KeilProjectXML;
import cn.xiuxius.embedded.util.JarPathUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.XmlException;
import picocli.CommandLine;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Callable;

@CommandLine.Command(name = "task",
        description = "Task Command",
        mixinStandardHelpOptions = true,
        subcommands = {
                BuildCommand.class,
                FlashCommand.class,
                CleanCommand.class,
                RunCommand.class,
                ProcessCommand.class
        }
)
@Slf4j
@Getter
public class TaskCommand implements Callable<Integer> {

    private EasyYamlConfig config;

    private String jarHome;

    private KeilProjectXML keilProjectXML;

    @CommandLine.Spec
    CommandLine.Model.CommandSpec spec;

    @CommandLine.Option(names = {"-w", "--working-dir"}, description = "Working directory", converter = ExistingDirectoryConverter.class)
    private Path workingDir;


    @Override
    public Integer call() {
        if (!spec.commandLine().getParseResult().hasSubcommand()) {
            CommandLine.usage(this, System.out);
            return 1;
        }
        return prepare();
    }


    public int prepare() {
        if (workingDir == null) {
            workingDir = Paths.get(System.getProperty("user.dir"));
        }
        jarHome = JarPathUtil.getJarHome();
        if (jarHome == null) {
            log.error("Please set the environment variable EASY_STC_HOME to the path of the project.");
            return 1;
        }

        EasyYamlConfig config = EasyYamlConfig.load();
        if (config == null) {
            log.error("Failed to load easy-stc.yml.");
            return 1;
        }
        Path uvprojPath;
        try {
            uvprojPath = Paths.get(config.getKeilProject().getUvprojPath());
        } catch (Exception e) {
            log.error("Failed to load uvproj file.");
            return 1;
        }
        if (uvprojPath.toString().isEmpty()) {
            log.error("Please specify the uvproj path.");
            return 1;
        }
        KeilProjectXML keilProjectXML;
        try {
            keilProjectXML = KeilProjectXML.loadUvproj(uvprojPath.toString());
        } catch (IOException | XmlException e) {
            log.error("Failed to parse uvopt file: {}", uvprojPath, e);
            return 1;
        }
        this.config = config;
        this.keilProjectXML = keilProjectXML;
        return 0;
    }



}
